package servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;
import managers.JSonManager;
import structures.User;

public class GetIdUserServlet extends HttpServlet {

	private static final long serialVersionUID = 1803870111332173849L;
	
	//method to read
		protected void doGet(HttpServletRequest request, HttpServletResponse response){
			String idUser, jsonResult;
			User user;
			JSonManager jSonManager;
			jSonManager = new JSonManager();
			DatabaseManager databaseManager;
			databaseManager = new DatabaseManager();
			idUser = request.getParameter("idUser");
			databaseManager.connect();
			user = databaseManager.getUserByID(idUser);
			databaseManager.closeConnection();
			jsonResult = jSonManager.generateJSON(user);
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			try {
				//write the result as JSon
				response.getWriter().write(jsonResult);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

}
